package com.bci.pwtz.service.impl;

import java.util.Date;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.remote.umeng.SendMsgResp;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.CompanyUserAuditMapper;
import com.bci.pwtz.mysql.model.Company;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.CompanyUserAudit;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.CompanyUserAuditService;
import com.bci.pwtz.service.CompanyUserService;
import com.bci.pwtz.service.umeng.UMengAppUtilService;
@Service
public class CompanyUserAuditServiceImpl implements CompanyUserAuditService
{
    private CompanyUserAuditMapper companyUserAuditMapper;
    private UMengAppUtilService umengAppUtilService;
    private CompanyUserService companyUserService;
    private CompanyService companyService;
    private static final Logger _logger=Logger.getLogger(CompanyUserAuditServiceImpl.class);
    
    public CompanyUserAuditMapper getCompanyUserAuditMapper()
    {
        return companyUserAuditMapper;
    }

    public void setCompanyUserAuditMapper(CompanyUserAuditMapper companyUserAuditMapper)
    {
        this.companyUserAuditMapper = companyUserAuditMapper;
    }


	public UMengAppUtilService getUmengAppUtilService() {
		return umengAppUtilService;
	}

	public void setUmengAppUtilService(UMengAppUtilService umengAppUtilService) {
		this.umengAppUtilService = umengAppUtilService;
	}

	public CompanyUserService getCompanyUserService() {
		return companyUserService;
	}

	public void setCompanyUserService(CompanyUserService companyUserService) {
		this.companyUserService = companyUserService;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	@Override
    public CompanyUserAudit add(CompanyUserAudit companyUserAudit) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("add:companyUserAudit="+companyUserAudit.toString());
        }
        if(companyUserAudit==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("add,companyUserAudit is null");
            }
            return null;
        }else{
            companyUserAudit.setCreateTime(new Date());
            companyUserAudit.setResult(1);
        }
        try{
            CompanyUserAudit checkCompanyUserAudit=companyUserAuditMapper.selectCompanyUserAuditByUserId(companyUserAudit.getCompanyUserId(), companyUserAudit.getCompanyId());
            if(checkCompanyUserAudit==null){
                companyUserAuditMapper.insertSelective(companyUserAudit);
                companyService.pushMessage(companyUserAudit.getCompanyUserId(), companyUserAudit.getCompanyId());
            }else{
                companyUserAudit.setResult(0);
            }
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        } 
        return companyUserAudit;
    }

    @Override
    public int update(CompanyUserAudit companyUserAudit) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("update:companyUserAudit="+companyUserAudit.toString());
        }
        if(companyUserAudit==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("update,companyUserAudit is null");
            }
            return 0;
        }
        int result=0;
        try{
            result=companyUserAuditMapper.updateByPrimaryKeySelective(companyUserAudit);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public int check(Long companyUserAuditId, int result) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("check:companyUserAuditId="+companyUserAuditId+"result="+result);
        }
        if(companyUserAuditId==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("check,companyUserAuditId is null");
            }
            return 0;
        }
        int r=0;
        try{
            r=companyUserAuditMapper.updateById(companyUserAuditId, result);
           CompanyUserAudit userAudit =  companyUserAuditMapper.selectByPrimaryKey(companyUserAuditId);
           CompanyUser userinfo = companyUserService.load(userAudit.getCompanyUserId());
           Company companyinfo  = companyService.load(userAudit.getCompanyId());
           String text ;
           if(result ==2 ){
        	    text = "尊敬的"+userinfo.getUserName()+",你已经通过"+companyinfo.getCompanyName()+"的审核,重新登录后可切换到该单位";
           }
           else {
        	    text = "尊敬的"+userinfo.getUserName()+",你未能通过"+companyinfo.getCompanyName()+"的审核,你可以联系该单位的管理员";
           }
          SendMsgResp resp =  umengAppUtilService.sendAndroidUnicast(userinfo.getDeviceId(), "配网消息", "审核通知", text, "go_app", "notification");
          _logger.debug("推送返回值为"+resp.getRet()+"\n");
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        } catch (Exception e) {
			e.printStackTrace();
		}
        return r;
    }

    @Override
    public void updateExpireStatus(int auditValid) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled())
        {
            _logger.debug("updateExpireStatus method start");
        }
        try
        {
            companyUserAuditMapper.updateExpireStatus(auditValid);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }        
    }

    @Override
    public int selectByUserId(Long userid,Long companyid) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("selectByUserId:userid="+userid+",companyid:"+companyid);
        }
        int r=0;
        try{
            r=companyUserAuditMapper.selectByUserId(userid,companyid);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return r;
    }

    @Override
    public CompanyUserAudit selectCompanyUserAuditByUserId(Long userid, Long companyid) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("selectByUserId:userid="+userid+",companyid:"+companyid);
        }
        CompanyUserAudit companyUserAudit=null;
        try{
            companyUserAudit=companyUserAuditMapper.selectCompanyUserAuditByUserId(userid,companyid);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return companyUserAudit;
    }

    @Override
    public CompanyUserAudit load(Long auditId) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("load: auditId="+auditId);
        }
        if(auditId==null||auditId==0){
            if(_logger.isDebugEnabled()){
                _logger.debug("load,auditId is null or 0");
            }
            return null;
        }
        CompanyUserAudit rtObj=null;
        try{
            rtObj=companyUserAuditMapper.selectByPrimaryKey(auditId);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return rtObj;
    }
    
    @Override
    public CompanyUserAudit selectCompanyUserAuditByAuditId(Long auditId) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("selectCompanyUserAuditByAuditId:auditId="+auditId);
        }
        CompanyUserAudit companyUserAudit= new CompanyUserAudit();
        try{
            companyUserAudit=companyUserAuditMapper.selectByPrimaryKey(auditId);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return companyUserAudit;
    }
    
    @Override
    public int delete(Long auditId) throws PwtzDatabaseException
    {
        if(auditId==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("delete,auditId is null");
            }
            return 0;
        }
        int result=0;
        try{
            result=companyUserAuditMapper.deleteByPrimaryKey(auditId);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
}
